<template>
  <a-modal
    title="移动到"
    :visible="visible"
    :centered="true"
    width="600px"
    :maskClosable="false"
    @cancel="handleCancel"
    @ok="handleOk"
    :afterClose="afterClose"
    destroyOnClose
  >
    <a-form-model ref="form" :model="form" v-bind="layout" :rules="rules">
      <a-form-model-item label="保存位置" prop="pid">
        <a-tree-select
          v-model="form.pid"
          placeholder="请选择保存位置"
          style="width: 100%"
          :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
          :tree-data="treeSelect.treeData"
          :replaceFields="treeSelect.replaceFields"
          @select="onTreeSelect"
        >
        </a-tree-select>
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>

<script>
import { upDashboardCategory, upDashboard, getDashboardCategoryTree } from "@/api/multidimensionalAnalysis"

const layout = {
  labelCol: { span: 4 },
  wrapperCol: { span: 20 }
}
export default {
  data() {
    return {
      visible:false,
      layout,
      form: {},
      rules: {
        // pid: [
        //   { required: true, message: '保存位置不得为空', trigger: 'change' },
        // ],
      },
      treeSelect: {
        treeData: [],
        replaceFields: {children:"children", title:"name", key:"id", value: "id" }
      },
      data: {},
      list: [],
      reset: false
    }
  },
  created() {
    // this.search()
  },
  methods: {
    search() {
      const iteration = (arr, path = [], disabled = false) => arr.map(val => {
          val.parentPath = path
          val.disabled = disabled || this.data.id === val.id
          if (val.children) {
            iteration(val.children, [...path, val.id], val.disabled )
          }
          return val
        })
      getDashboardCategoryTree()
      .then(res => {
        if (res.errorCode === 0) {
          this.treeSelect.treeData = iteration(res.data)
        } else {
          this.$message.error(res.msg)
        }
      })
      .catch(err => {
        this.$message.error(err)
      })
    },
    onTreeSelect(value, node, extra) {
      this.data.parentPath = node.dataRef.parentPath.join(",")
    },
    show(list, reset = false) {
      this.reset = reset
      this.visible = true
      // this.data = JSON.parse(JSON.stringify(val))
      this.list = [...list]
      this.search()
    },
    handleCancel() {
      this.visible = false
      this.$emit("move")
    },
    handleOk() {
      // const pid = this.form.pid == undefined ? 0 : this.form.id
      // const params = Object.assign(this.data, {pid})
      Promise.all(this.list.map(item => {
        const params = {
          ...item,
        }
        let callBack
        if(item.type === 2){
          params.dasDashboardCategoryId = this.form.pid || 0
          callBack = upDashboard
        }
        else {
          params.pid = this.form.pid || 0
          callBack = upDashboardCategory
        }
        return callBack(params)
      }))
      .then(resList => {
        if(!resList.find(res => res.code !== "000000")){
          this.$message.success("移动成功")

        }
        else {
          const errList = resList.filter(res => res.code !== "000000")
          this.$message.success(`${errList.join("、")}提交失败`)
        }
        this.handleCancel()
      })
      // upDashboardCategory(params)
      // .then(res => {
      //   if (res.code === '000000') {
      //     if (this.reset) {
      //       this.$emit('reset')
      //     } else {
      //       this.$emit('refresh')
      //     }
      //     this.handleCancel()
      //   } else {
      //     this.$message.error(res.msg)
      //   }
      // })
      // .catch(err => {
      //   this.$message.error(err)
      // })
    },
    afterClose() {
      this.form = {}
      this.treeSelect.treeData = []
      this.data = {}
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .ant-col{
  margin: 0!important;
  padding: 0!important;
}
</style>
